home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gplibt02.zoo / tfix24.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-28  |  2.5 KB  |  110 lines

  1. //
  2. // testFix24.cc : test Fix24/48 classes
  3. //
  4.  
  5. #include <xfix24.h>
  6.  
  7. // This a set of inlines instead of a macro to force the side effects
  8. // of evaluating y to happen before x is printed.
  9.  
  10. inline void check(char *x, int y) { cout << x << " = " << (y) << "\n"; }
  11. inline void check(char *x, double y) { cout << x << " = " << (y) << "\n"; }
  12. inline void check(char *x, Fix24 y) { cout << x << " = " << (y) << "\n"; }
  13. inline void check(char *x, Fix48 y) { cout << x << " = " << (y) << "\n"; }
  14.  
  15. void test24() {
  16.   cout << "Fix24: identities should be displayed\n";
  17.  
  18.   Fix24 a;        check("0",a);
  19.   Fix24 b = .5;        check(".5",b);
  20.   Fix24 c = -.5;    check("-.5",c);
  21.   Fix24 d = .1;        check(".1",d);
  22.   Fix24 e = b;        check(".5",e);
  23.  
  24.   check(".5",a = b);
  25.   check(".25",a = .25);
  26.   check("536870912",mantissa(a));
  27.   mantissa(a)=536870912;
  28.   check(".25",a);
  29.   check(".25",value(a));
  30.  
  31.   check(".25",+a);
  32.   check("-.25",-a);
  33.  
  34.   check(".1 + .5",d+b);
  35.   check(".1 - .5",d-b);
  36.   check(".1 * .5",d*b);
  37.   check(".1 *  3",d*3);
  38.   check(".1 * -3",d*-3);
  39.   check(".1 / .5",d/b);
  40.   check(".1 << 1",d<<1);
  41.   check("-.5 >> 2",c>>2);
  42.  
  43.   check(".1 == .5",d == b);
  44.   check(".1 != .5",d != b);
  45.   check(".1 > .5",d > b);
  46.   check(".5 <= -.5",b <= c);
  47.  
  48.   cout << "Fix24: range errors ignored and overflows saturated\n";
  49.   set_Fix24_overflow_handler(Fix24_overflow_saturate);
  50.   set_Fix24_range_error_handler(Fix24_ignore);
  51.  
  52.   Fix24 f = 1.1;    check("1.1",f);
  53.  
  54.   Fix24 g = .7;
  55.   check(".7 + .5",g+b);
  56.   check("-.5 - .7",c-g);
  57.   check(".5 / .1",b/d);
  58. }
  59.  
  60. void test48() {
  61.   cout << "Fix48: identities should be displayed\n";
  62.  
  63.   Fix48 a;        check("0",a);
  64.   Fix48 b = .5;        check(".5",b);
  65.   Fix48 c = -.5;    check("-.5",c);
  66.   Fix48 d = .1;        check(".1",d);
  67.   Fix48 e = b;        check(".5",e);
  68.  
  69.   check(".5",a = b);
  70.   check(".25",a = .25);
  71.   twolongs t;
  72.   t = mantissa(a);
  73.   check("536870912",t.u);
  74.   check("0",t.l);
  75.   mantissa(a)=t;
  76.   check(".25",a);
  77.   check(".25",value(a));
  78.  
  79.   check(".25",+a);
  80.   check("-.25",-a);
  81.  
  82.   check(".1 + .5",d+b);
  83.   check(".1 - .5",d-b);
  84.   check(".1 *  3",d*3);
  85.   check(".1 * -3",d*-3);
  86.   check(".1 << 1",d<<1);
  87.   check("-.5 >> 2",c>>2);
  88.  
  89.   check(".1 == .5",d == b);
  90.   check(".1 != .5",d != b);
  91.   check(".1 > .5",d > b);
  92.   check(".5 <= -.5",b <= c);
  93.  
  94.   cout << "Fix48: range errors reported and overflows reported\n";
  95.   set_Fix48_overflow_handler(Fix48_warning);
  96.   set_Fix48_range_error_handler(Fix48_warning);
  97.  
  98.   Fix48 f = 1.1;    check("1.1",f);
  99.  
  100.   Fix48 g = .7;
  101.   check(".7 + .5",g+b);
  102.   check("-.5 - .7",c-g);
  103. }
  104.  
  105. main() {
  106.   test24();
  107.   test48();
  108. }
  109.  
  110.